package com.sg.plmadapter.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bson.types.ObjectId;

import com.mobnut.db.model.ModelService;
import com.sg.business.model.Document;
import com.sg.business.model.Folder;
import com.sg.business.model.Project;

public class ProductNumToProductItem extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String id = req.getParameter("id");
		ObjectId objectId = new ObjectId(id);
		Document document = ModelService.createModelObject(Document.class,
				objectId);
		Folder folder = document.getFolder();
		ObjectId projectId = folder.getProject_id();
		if (projectId != null) {
			Project project = ModelService.createModelObject(Project.class,
					projectId);
			try {
				project.doAddProduct((String)(document.getValue(Document.F_PRODUCT_NUMBER)),
						null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
